package com.example.siyun.controller.usercontroller;

import com.example.siyun.pojo.Apply;
import com.example.siyun.service.userserver.ApplyService;
import com.example.siyun.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
/**
 * @Author 赵薇
 * @Date 2022/10/12 15:02
 * @PackageName:com.example.siyun.controller
 * @ClassName: ApplyController  审核管理列表控制层
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Slf4j
public class ApplyController {

    @Autowired
    private ApplyService applyServer;


    /**
     * 审核列表全部查询
     * @param page
     * @param limit
     * @param applyName
     * @param applyAccount
     * @param applyTime
     * @return
     */
    @RequestMapping("/Primary/getApp")
    public PageResult getApp(@RequestParam(defaultValue = "10") Integer page, @RequestParam(defaultValue = "1") Integer limit,
                             String applyName, String applyAccount, String  applyTime){

        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<Apply> app = applyServer.getApp(applyAccount, applyName, applyTime);
        PageInfo<Apply> pageInfo = new PageInfo<>(app);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    /**
     * 条件查询(已批准，未审核，已驳回)
     * @param page
     * @param limit
     * @param applyState
     * @param applyName
     * @param applyAccount
     * @return
     */
    @RequestMapping("/Primary/getAudit")
    public PageResult getAudit(@RequestParam(defaultValue = "10") Integer page,@RequestParam(defaultValue = "1") Integer limit,
                               String applyState,String applyName, String applyAccount){

        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<Apply> app = applyServer.getAudit(applyState,applyName,applyAccount);
        PageInfo<Apply> pageInfo = new PageInfo<>(app);

        PageResult pageResult = new PageResult();
        pageResult.setData(pageInfo.getList());
        pageResult.setMessage("成功");
        pageResult.setState(0);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;

    }

    /**
     * 审核管理列表中未审核按钮
     * @param applyId
     * @param applyState
     * @return
     */
    @RequestMapping("/Primary/audit")
    public PageResult update(@RequestBody String[] applyId,String applyState){
        log.info("未审核：applyId:{},applyState:{}",applyId,applyState);
        for (String string : applyId) {
            String sh = "未审核";
            applyServer.update(string,sh);
        }

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("未审核：applyId:{},applyState:{},msg:{}",applyId,applyState,result.getMessage());
        return result;
    }

    /**
     * 审核管理中已批准按钮
     * @param applyId
     * @param applyState
     * @return
     */
    @RequestMapping("/Primary/through")
    public PageResult through(@RequestBody String[] applyId,String applyState){
        log.info("已批准：applyId:{},applyState:{}",applyId,applyState);
        for (String string : applyId) {
            String pz = "已批准";
            applyServer.update(string,pz);
        }

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("已批准：applyId:{},applyState:{},msg:{}",applyId,applyState,result.getMessage());
        return result;
    }

    /**
     * 审核管理中已驳回按钮
     * @param applyId
     * @return
     */
    @RequestMapping("/Primary/rejected")
    public PageResult rejected(@RequestBody String[] applyId){
        log.info("已批准：applyId:{}",applyId);
        for (String string : applyId) {
            String bh = "已驳回";
            applyServer.update(string,bh);
        }

        PageResult result = new PageResult();
        result.setMessage("成功");
        log.info("已批准：applyId:{},msg:{}",applyId,result.getMessage());
        return result;
    }

}
